/*
 * BiunitPoint.java
 * 
 */

package matchstiklib;

/**
 * Represents a point in the buinit space [-1,1]
 * 
 * @author Jon Ludwig
 */
public class FPoint {
    
    public double x, y;
    public double xrange, yrange;

    public FPoint(double _xrange, double _yrange) {
        x = 0;
        y = 0;
        xrange = _xrange;
        yrange = _yrange;
    }

    /**
     * Returns the polar radius
     * 
     * @return polar radius
     */
    public double r()
    {
        return Math.hypot(x, y); //Math.sqrt(x*x + y*y);
    }
    
    /**
     * Returns the polar radius squared
     * 
     * @return polar radius squared
     */
    public double r2()
    {
        return x*x + y*y;
    }
    
    /**
     * Returns the polar angle
     * 
     * @return polar angle
     */
    public double theta()
    {
        return Math.atan2(y, x);
    }
    
    /**
     * Generates a point in the biunit space [-1,1] using a uniform distribution
     * 
     * @return random point
     */
    public static FPoint Random(double xrange, double yrange)
    {
        FPoint p = new FPoint(xrange, yrange);
        
        p.x = (Math.random() - 0.5) * (xrange/0.5);
        p.y = (Math.random() - 0.5) * (yrange/0.5);
        
        return p;
    }
    
    public int scaleX(int width)
    {
        double xs;
        
        xs = x + xrange;
        xs *= Math.floor((double)(width-1)/(xrange*2));
        
        return (int)xs;
    }
    
    public int scaleY(int height)
    {
        double ys;
        
        ys = y + yrange;
        ys *= Math.floor((double)(height-1)/(yrange*2));
        
        return (int)ys;
    }
}
